package br.com.etecmatao.admin.controller;

import br.com.etecmatao.business.BusinessLocator;
import br.com.etecmatao.business.exception.BusinessValidateException;
import br.com.etecmatao.business.impl.BusinessLocatorImpl;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@RequestScoped
public class Login {

    private BusinessLocator businessLocator;
    private br.com.etecmatao.model.Login objLogin;

    public Login() {
        objLogin = new br.com.etecmatao.model.Login();
        businessLocator = new BusinessLocatorImpl();
    }

    public br.com.etecmatao.model.Login getObjLogin() {
        return objLogin;
    }

    public void setObjLogin(br.com.etecmatao.model.Login objLogin) {
        this.objLogin = objLogin;
    }

    public String logar() throws BusinessValidateException {
        objLogin = businessLocator.getLoginBusiness().logar(objLogin);
        if (objLogin != null) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", objLogin);
            return "index";
        }
        return null;
    }

    public String logout() {
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", null);
        return "login";
    }

    public void save() {
        businessLocator.getLoginBusiness().save(objLogin);
    }
}
